<?php

require_once 'lib.php';

/**
 * Description of MarksRetriever
 *
 * @author markus
 */
class MarksRetriever {
    private $connection;
    private $url;
    private $result;
    
    function __construct(ActionsConnection $connection, $locale = "de"){
        $this->url = "https://actions.fh-joanneum.at/st/listMarks.do?locale=".$locale;
        $this->connection = $connection;
        
        logger("Marks", $this->connection->status, $this->connection->key);
    }
    
    public function retrieve(){
        $e = curl_init($this->url);
        curl_setopt($e, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($e, CURLOPT_COOKIEFILE, $this->connection->getFile());
        curl_setopt($e, CURLOPT_COOKIEJAR, $this->connection->getFile());
        curl_setopt($e, CURLOPT_HEADER, 0);

        $content = curl_exec($e);

        curl_close($e);
        
        $this->result = $content;
    }
    
    public function toXml(){
        $content = $this->result;
        $content = explode("marksTable", $content);
        $content = '<table cellpadding="3" cellspacing="0" class="list" id="'.$content[1];

        $content = explode('<div class="fnotes">', $content);

        $content = trim($content[0]);

        $content = str_replace("&nbsp;", "", $content);
        $content = str_replace("\t", "", $content);
        $content = str_replace("\n", "", $content);
        $content = str_replace("\r", "", $content);
        $content = str_replace("</td></td>", "</td>", $content);
        $content = str_replace("&Uuml;", "Ü", $content);
        $content = str_replace("&uuml;", "ü", $content);
        $content = str_replace("&Auml;", "Ä", $content);
        $content = str_replace("&auml;", "ä", $content);
        $content = str_replace("&Ouml;", "Ö", $content);
        $content = str_replace("&ouml;", "ö", $content);
        $content = str_replace("&szlig;", "ß", $content);
        $content = str_replace("&", "und", $content);

        $content = '<?xml version="1.0"?>'.$content;

        $content = strip_tags($content, "<table><tr><td><th>");

        $xmlObj = simplexml_load_string($content);
        $fmarr = objectsIntoArray($xmlObj);

        $aktuell = $fmarr['tr'];

        $grades = "";

        foreach($aktuell as $note){
            $tfach = $note['td'][0];
            $tnote = $note['td'][2];
            $tnoteganz = $note['td'][3];

            if($tfach != null && $tnote != null){
                $grades[$key][] = array($note['td'][0], $note['td'][2], $note['td'][3]);
            }

            if($note['th'] != '' && !is_array($note['th'])){
                $key = $note['th'];
            }
        }  

        $result = '<?xml version="1.0"?>';
        $result .= '<Overview>';
        $result .= '<Status>'.$this->connection->status.'</Status>';

        $terms = array_keys($grades);
        foreach($terms as $term){   
        $result .= '<Term name="'.$term.'">';
            foreach($grades[$term] as $grade){
                $result .= '<Course>';
                    $result .= '<Title>'.$grade[0].'</Title>';
                    $result .= '<Grade>'.$grade[1].'</Grade>';
                    $result .= '<GradeWords>'.$grade[2].'</GradeWords>';
                $result .= '</Course>';
            }
        $result .= '</Term>';
        }
        $result .= '</Overview>';
        
        return $result;
    }
}

?>
